﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Medical.Business;
using Medical.Entity;
using System.IO;

/// <summary>
/// 分类操作页面
/// </summary>
public partial class DoSortInfo : AdminPage
{
    /// <summary>
    /// ID
    /// </summary>
    protected int nID;

    /// <summary>
    /// 分类类型
    /// </summary>
    protected ArticelTypeEnum eType;

    /// <summary>
    /// 操作类型
    /// </summary>
    protected string strType = string.Empty;

    /// <summary>
    /// 分类级别
    /// </summary>
    protected int nSortCount = 0;

    /// <summary>
    /// 图片大小
    /// </summary>
    protected string strImgSize = string.Empty;

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Common.GetQueryValue("type", 0);
        eType = (ArticelTypeEnum)Convert.ToInt32(Common.GetQueryValue("infotype", 0));
        nSortCount = Convert.ToInt32(Common.GetQueryValue("count", 0));
        strImgSize = WebConfigInfo.FileSize < 1024 * 1024 ? (WebConfigInfo.FileSize / 1024 + "K") : (WebConfigInfo.FileSize / (1024 * 1024) + "M");
        int.TryParse(Common.GetQueryValue("id", 0),out nID);
        if (!Page.IsPostBack)
        {
            InitSort();
        }
        switch (eType)
        {
            case ArticelTypeEnum.Disease:
                ImgDiv.Visible = true;
                break;
        }
        if (strType == "edit")
        {
            if (!Page.IsPostBack)
            {
                //页面数据初始化
                InitData();
            }
        }
    }

    /// <summary>
    /// 初始化分类下拉框
    /// </summary>
    protected void InitSort()
    {
        //绑定分类下拉框
        ddlSort.Items.Clear();
        if (eType != ArticelTypeEnum.Health)
        {
            ddlSort.Items.Add(new ListItem("父菜单", "0"));
        }
        if (nSortCount > 1)
        {
            BisSortInfo bisSortInfo = new BisSortInfo();
            DataTable dtSortList = bisSortInfo.GetAllSortList(eType);
            if (dtSortList != null && dtSortList.Rows.Count > 0)
            {
                int nSortIndex = 0;
                foreach (DataRow dr in dtSortList.Rows)
                {
                    nSortIndex = Convert.ToInt32(dr["SortIndex"]);
                    if (nSortCount == nSortIndex && nSortCount > 1)
                        continue;
                    if (Convert.ToInt32(dr["ID"]) == nID)
                        continue;
                    ddlSort.Items.Add(new ListItem(Common.GetSortIcon(nSortIndex) + dr["Name"].ToString() + "子类", dr["ID"].ToString()));
                }
                dtSortList.Dispose();
            }
        }
        else
        {
            ParentSortTD.Style["display"] = "none";
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.Sort;
    }

    /// <summary>
    /// 页面数据初始化
    /// </summary>
    protected void InitData()
    {
        //获取分类信息
        SortInfo Info = new BisSortInfo().Get(nID);
        if (Info != null)
        {
            txtName.Text = Info.Name;
            ddlSort.SelectedValue = Info.ParentID.ToString();
            txtPageUrlName.Text = Info.PageUrlName;
            txtPageTitle.Text = Info.PageTitle;
            txtPageKeywords.Text = Info.PageKeywords;
            txtPageDescription.Text = Info.PageDescription;
            hfImg.Value = Info.ImgPlace;
            if (Info.ParentID.Value == 0)
                ParentSortTD.Style["display"] = "none";
            switch (eType)
            {
                case ArticelTypeEnum.Disease:
                    ImgPanel.Visible = true;
                    imgAdver.ImageUrl = "~/" + WebConfigInfo.FilePlace + "d/" + Info.ImgPlace;
                    break;

            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool result = false;
        BisSortInfo bisSortInfo = new BisSortInfo();
        SortInfo Info = new SortInfo();
        Info.Name= txtName.Text.Trim();
        Info.ParentID = Convert.ToInt32(ddlSort.SelectedValue);
        Info.Type = (int)eType;
        Info.PageUrlName = txtPageUrlName.Text.Trim();
        Info.PageTitle = txtPageTitle.Text.Trim();
        Info.PageKeywords = txtPageKeywords.Text.Trim();
        Info.PageDescription = txtPageDescription.Text.Trim();
        string strImg = string.Empty;
        if (bisSortInfo.CheckPageUrlName(nID, Info.PageUrlName))
        {
            Until.Alert("Url重写名称已经被占用！");
            txtPageUrlName.Focus();
            return;
        }

        if (eType == ArticelTypeEnum.Disease)
        {
            //上传图片
            if (fuImg.HasFile)
            {
                result = UpImg(fuImg, ref strImg);
                if (!result)
                    return;
            }
            else if (strType == "add")
            {
                Until.Alert("请选择要上传的图片！");
                return;
            }
        }
        if (strType == "add")
        {
            //新增信息
            Info.ImgPlace = strImg;
            //新增分类
            int nTemID = bisSortInfo.InsertSort(Info);
            result = nTemID > 0;
            if (result && eType == ArticelTypeEnum.Disease && Info.ParentID.Value == 0)
            {
                Banner bannerInfo=new Banner();
                bannerInfo.BannerType = (int)AdverTypeEnum.Banner;
                bannerInfo.OrderNum = nTemID;
                new BisBanner().Insert(bannerInfo);
            }
        }
        else if (strType == "edit")
        {
            Info.ImgPlace = (strImg == string.Empty ? hfImg.Value : strImg);
            //编辑分类
            Info.ID = nID;
            result = bisSortInfo.Update(Info);
            if (eType == ArticelTypeEnum.Disease)
            {
                if (result && !string.IsNullOrEmpty(strImg))
                    Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + hfImg.Value));
            }
        }
        //结果处理
        if (result)
        {
            Common.dtDiseaseSort = null;
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }
    }

    /// <summary>
    /// 生成拼音
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLetter_Click(object sender, EventArgs e)
    {
        txtPageUrlName.Text = Common.GetSpell(txtName.Text.Trim());
    }

    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="fileImg"></param>
    /// <param name="strImg"></param>
    /// <returns></returns>
    protected bool UpImg(FileUpload fileImg, ref string strImg)
    {
        string strExtension = Path.GetExtension(fileImg.FileName).ToLower();
        if (fileImg.PostedFile.ContentLength > WebConfigInfo.FileSize)
        {
            Until.Alert("图片大小不超过" + strImgSize + "！");
            return false;
        }
        //验证图片格式
        if (Until.CheckFileType(0, strExtension))
        {
            string strFileName = DateTime.Now.ToString("MMddHHmmss") + strExtension;
            string strYear = DateTime.Now.Year.ToString();

            //原图
            string strFile = Server.MapPath("~/" + WebConfigInfo.FilePlace + "/d/" + strYear + "/");
            if (!Directory.Exists(strFile))
            {
                Directory.CreateDirectory(strFile);
            }
            fileImg.SaveAs(strFile + strFileName);

            ////小图
            //string[] strSmallSize = WebConfigInfo.CaseSmallImg.Split('*');
            //System.Drawing.Image SmallImage = Common.MakeMiImage(fileImg.PostedFile, Convert.ToInt32(strSmallSize[0]), Convert.ToInt32(strSmallSize[1]));
            //strFile = Server.MapPath("~/" + WebConfigInfo.FilePlace + "/s/" + strYear + "/");
            //if (!Directory.Exists(strFile))
            //{
            //    Directory.CreateDirectory(strFile);
            //}
            //SmallImage.Save(strFile + strFileName);
            //SmallImage.Dispose();

            strImg = strYear + "/" + strFileName;
            return true;
        }
        else
        {
            Until.Alert("图片格式不正确！");
            return false;
        }
    }
}
